﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x02000145 RID: 325
[DisallowMultipleComponent]
[RequireComponent(typeof(VehicleParent))]
[AddComponentMenu("RVP/C#/Stunt/Stunt Detector", 1)]
public class StuntDetect : MonoBehaviour
{
	// Token: 0x060005E0 RID: 1504 RVA: 0x0002EA7C File Offset: 0x0002CC7C
	private void Start()
	{
		this.tr = base.transform;
		this.rb = base.GetComponent<Rigidbody>();
		this.vp = base.GetComponent<VehicleParent>();
	}

	// Token: 0x060005E1 RID: 1505 RVA: 0x0002EAA4 File Offset: 0x0002CCA4
	private void FixedUpdate()
	{
		if (this.detectDrift && !this.vp.crashing)
		{
			this.DetectDrift();
		}
		else
		{
			this.drifting = false;
			this.driftDist = 0f;
			this.driftScore = 0f;
			this.driftString = string.Empty;
		}
		if (this.detectJump && !this.vp.crashing)
		{
			this.DetectJump();
		}
		else
		{
			this.jumpTime = 0f;
			this.jumpDist = 0f;
			this.jumpString = string.Empty;
		}
		if (this.detectFlips && !this.vp.crashing)
		{
			this.DetectFlips();
		}
		else
		{
			this.stunts.Clear();
			this.flipString = string.Empty;
		}
		this.stuntString = ((!this.vp.crashing) ? (this.driftString + this.jumpString + ((!string.IsNullOrEmpty(this.flipString) && !string.IsNullOrEmpty(this.jumpString)) ? " + " : string.Empty) + this.flipString) : "Crashed");
	}

	// Token: 0x060005E2 RID: 1506 RVA: 0x0002EBE8 File Offset: 0x0002CDE8
	private void DetectDrift()
	{
		this.endDriftTime = ((this.vp.groundedWheels <= 0) ? 0f : ((Mathf.Abs(this.vp.localVelocity.x) <= 5f) ? Mathf.Max(0f, this.endDriftTime - Time.timeScale * TimeMaster.inverseFixedTimeFactor) : StuntManager.driftConnectDelayStatic));
		this.drifting = (this.endDriftTime > 0f);
		if (this.drifting)
		{
			this.driftScore += StuntManager.driftScoreRateStatic * Mathf.Abs(this.vp.localVelocity.x) * Time.timeScale * TimeMaster.inverseFixedTimeFactor;
			this.driftDist += this.vp.velMag * Time.fixedDeltaTime;
			this.driftString = "Drift: " + this.driftDist.ToString("n0") + " m";
			if (this.engine)
			{
				this.engine.boost += StuntManager.driftBoostAddStatic * Mathf.Abs(this.vp.localVelocity.x) * Time.timeScale * 0.0002f * TimeMaster.inverseFixedTimeFactor;
			}
		}
		else
		{
			this.score += this.driftScore;
			this.driftDist = 0f;
			this.driftScore = 0f;
			this.driftString = string.Empty;
		}
	}

	// Token: 0x060005E3 RID: 1507 RVA: 0x0002ED78 File Offset: 0x0002CF78
	private void DetectJump()
	{
		if (this.vp.groundedWheels == 0)
		{
			this.jumpDist = Vector3.Distance(this.jumpStart, this.tr.position);
			this.jumpTime += Time.fixedDeltaTime;
			this.jumpString = "Jump: " + this.jumpDist.ToString("n0") + " m";
			if (this.engine)
			{
				this.engine.boost += StuntManager.jumpBoostAddStatic * Time.timeScale * 0.01f * TimeMaster.inverseFixedTimeFactor;
			}
		}
		else
		{
			this.score += (this.jumpDist + this.jumpTime) * StuntManager.jumpScoreRateStatic;
			if (this.engine)
			{
				this.engine.boost += (this.jumpDist + this.jumpTime) * StuntManager.jumpBoostAddStatic * Time.timeScale * 0.01f * TimeMaster.inverseFixedTimeFactor;
			}
			this.jumpStart = this.tr.position;
			this.jumpDist = 0f;
			this.jumpTime = 0f;
			this.jumpString = string.Empty;
		}
	}

	// Token: 0x060005E4 RID: 1508 RVA: 0x0002EEC0 File Offset: 0x0002D0C0
	private void DetectFlips()
	{
		if (this.vp.groundedWheels == 0)
		{
			foreach (Stunt stunt in StuntManager.stuntsStatic)
			{
				if (Vector3.Dot(this.vp.localAngularVel.normalized, stunt.rotationAxis) >= stunt.precision)
				{
					bool flag = false;
					foreach (Stunt stunt2 in this.stunts)
					{
						if (stunt.name == stunt2.name)
						{
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						this.stunts.Add(new Stunt(stunt));
					}
				}
			}
			foreach (Stunt stunt3 in this.stunts)
			{
				if (Vector3.Dot(this.vp.localAngularVel.normalized, stunt3.rotationAxis) >= stunt3.precision)
				{
					stunt3.progress += this.rb.angularVelocity.magnitude * Time.fixedDeltaTime;
				}
				if (stunt3.progress * 57.29578f >= stunt3.angleThreshold)
				{
					bool flag2 = false;
					foreach (Stunt stunt4 in this.doneStunts)
					{
						if (stunt4 == stunt3)
						{
							flag2 = true;
							break;
						}
					}
					if (!flag2)
					{
						this.doneStunts.Add(stunt3);
					}
				}
			}
			string text = string.Empty;
			this.flipString = string.Empty;
			foreach (Stunt stunt5 in this.doneStunts)
			{
				text = ((stunt5.progress * 57.29578f < stunt5.angleThreshold * 2f) ? string.Empty : (" x" + Mathf.FloorToInt(stunt5.progress * 57.29578f / stunt5.angleThreshold).ToString()));
				this.flipString = ((!string.IsNullOrEmpty(this.flipString)) ? (this.flipString + " + " + stunt5.name + text) : (stunt5.name + text));
			}
		}
		else
		{
			foreach (Stunt stunt6 in this.stunts)
			{
				this.score += stunt6.progress * 57.29578f * stunt6.scoreRate * (float)Mathf.FloorToInt(stunt6.progress * 57.29578f / stunt6.angleThreshold) * stunt6.multiplier;
				if (this.engine)
				{
					this.engine.boost += stunt6.progress * 57.29578f * stunt6.boostAdd * stunt6.multiplier * 0.01f;
				}
			}
			this.stunts.Clear();
			this.doneStunts.Clear();
			this.flipString = string.Empty;
		}
	}

	// Token: 0x040008B1 RID: 2225
	private Transform tr;

	// Token: 0x040008B2 RID: 2226
	private Rigidbody rb;

	// Token: 0x040008B3 RID: 2227
	private VehicleParent vp;

	// Token: 0x040008B4 RID: 2228
	[NonSerialized]
	public float score;

	// Token: 0x040008B5 RID: 2229
	private List<Stunt> stunts = new List<Stunt>();

	// Token: 0x040008B6 RID: 2230
	private List<Stunt> doneStunts = new List<Stunt>();

	// Token: 0x040008B7 RID: 2231
	private bool drifting;

	// Token: 0x040008B8 RID: 2232
	private float driftDist;

	// Token: 0x040008B9 RID: 2233
	private float driftScore;

	// Token: 0x040008BA RID: 2234
	private float endDriftTime;

	// Token: 0x040008BB RID: 2235
	private float jumpDist;

	// Token: 0x040008BC RID: 2236
	private float jumpTime;

	// Token: 0x040008BD RID: 2237
	private Vector3 jumpStart;

	// Token: 0x040008BE RID: 2238
	public bool detectDrift = true;

	// Token: 0x040008BF RID: 2239
	public bool detectJump = true;

	// Token: 0x040008C0 RID: 2240
	public bool detectFlips = true;

	// Token: 0x040008C1 RID: 2241
	private string driftString;

	// Token: 0x040008C2 RID: 2242
	private string jumpString;

	// Token: 0x040008C3 RID: 2243
	private string flipString;

	// Token: 0x040008C4 RID: 2244
	[NonSerialized]
	public string stuntString;

	// Token: 0x040008C5 RID: 2245
	public Motor engine;
}
